package com.hzhc.controller.yworder;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hzhc.annotation.SysTemLog;
import com.hzhc.common.BaseController;
import com.hzhc.dto.UserDto;
import com.hzhc.enumBean.ResultUtilCodeEnum;
import com.hzhc.service.SysUserService;
import com.hzhc.service.YwOrderService;
import com.hzhc.utils.ResultUtil;
import com.hzhc.utils.TokenUtil;
import com.hzhc.vo.TrainPlanDetailVo;
import com.hzhc.vo.YwOrderCarVo;
import com.hzhc.vo.YwOrderVo;
import lombok.RequiredArgsConstructor;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 业务委托
 *
 * @author Fchenxu
 * @since 2024-05-27
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/ywOrder")
public class YwOrderController extends BaseController {
    @Value("${file.ywOderFileUploadFolder}")
    private String ywOderFileUploadFolder;
    @Value("${file.ywOderFileAccessPath}")
    private String ywOderFileAccessPath;
    private final YwOrderService ywOrderService;
    @Value("${cms.targetUrl}")
    private String targetUrl;
    private final SysUserService sysUserService;

    /**
     * 查询
     */
    @RequestMapping("/getOrderList")
    public ResultUtil<List<YwOrderVo>> getOrderList(@RequestBody YwOrderVo vo) {
        Page<YwOrderVo> page = new Page<>(vo.getPageIndex(), vo.getPageSize());
        List<YwOrderVo> vos = ywOrderService.getOrderPage(page, vo);
        return ResultUtil.build(vos,page.getTotal(),page.getPages(),ResultUtilCodeEnum.SUCCESS);
    }

    /**
     * 新增
     */
    @PostMapping("/addOrder")
    public ResultUtil<T> addOrder(@RequestBody YwOrderVo vo, HttpServletRequest request) {
        UserDto user = TokenUtil.getUser(request);
        vo.setUserId(user.getId());
        vo.setUserName(user.getRealname());
        vo.setCreateTime(LocalDateTime.now());
        vo.setAccessPath("CMS");
        return ywOrderService.addOrder(vo,targetUrl);
    }

    /**
     * 编辑
     */
    @PostMapping("/updateOrder")
    public ResultUtil<T> updateOrder(@RequestBody YwOrderVo vo, HttpServletRequest request) {
        UserDto user = TokenUtil.getUser(request);
        vo.setUpdateUserId(user.getId());
        vo.setUpdateUserName(user.getRealname());
        vo.setUpdateTime(LocalDateTime.now());
        return ywOrderService.updateOrder(vo);
    }

    /**
     * 删除
     */
    @RequestMapping("/deleteOrder/{id}")
    public ResultUtil<T> deleteOrder(@PathVariable("id") Integer id) {
        return ywOrderService.deleteOrder(id);
    }


    /**
     * 审核
     */
    @PostMapping("/updateCheck")
    public ResultUtil<T> updateCheck(@RequestBody YwOrderVo vo, HttpServletRequest request) {
        UserDto user = TokenUtil.getUser(request);
        vo.setUpdateUserId(user.getId());//cms操作人-审核人id
        vo.setAuditUserName(user.getRealname());
        return ywOrderService.updateCheck(vo);
    }

    /**
     * 获取堆存计划列表
     */
    @PostMapping("/getYwOrderList")
    public ResultUtil<List<YwOrderVo>> getYwOrderList(@RequestBody YwOrderVo vo){
        Page<YwOrderVo> page = new Page<>(vo.getPageIndex(),vo.getPageSize());
        List<YwOrderVo> list = ywOrderService.getYwOrderList(page,vo);
        return ResultUtil.build(list,page.getTotal(),page.getPages(), ResultUtilCodeEnum.SUCCESS);
    }

    /**
     * 堆存计划
     */
    @PostMapping("/updateYwOrderArea")
    @SysTemLog(operModul = "委托受理-安排堆存计划",operType = "UPDATE",operDesc = "安排堆存计划",modular="PC")
    public ResultUtil<T> updateYwOrderArea(HttpServletRequest request, @RequestBody YwOrderVo vo){
        UserDto user = TokenUtil.getUser(request);
        vo.setUpdateTime(LocalDateTime.now());
        vo.setUpdateUserId(user.getId());
        vo.setUpdateUserName(user.getRealname());
        return ywOrderService.updateYwOrderArea(vo);
    }

//    /**
//     * 新增提空重进数据
//     */
//    @PostMapping("/addTkzjOrder")
//    public ResultUtil<T> addTkzjOrder(@RequestBody YwOrderVo vo, HttpServletRequest request) {
//        UserDto user = TokenUtil.getUser(request);
//        vo.setUserId(user.getId());
//        vo.setUserName(user.getRealname());
//        vo.setCreateTime(LocalDateTime.now());
//        return ywOrderService.addTkzjOrder(request,vo);
//    }
//
//    /**
//     * 编辑提空重进数据
//     */
//    @PostMapping("/updateTkzjOrder")
//    public ResultUtil<T> updateTkzjOrder(@RequestBody YwOrderVo vo, HttpServletRequest request) {
//        UserDto user = TokenUtil.getUser(request);
//        vo.setUpdateUserId(user.getId());
//        vo.setUpdateUserName(user.getRealname());
//        vo.setUpdateTime(LocalDateTime.now());
//        return ywOrderService.updateTkzjOrder(vo);
//    }
//
//    /**
//     * 新增提重委托
//     */
//    @PostMapping("/addTzOrder")
//    public ResultUtil<T> addTzOrder(@RequestBody YwOrderVo vo, HttpServletRequest request) {
//        UserDto user = TokenUtil.getUser(request);
//        vo.setUserId(user.getId());
//        vo.setUserName(user.getRealname());
//        vo.setCreateTime(LocalDateTime.now());
//        return ywOrderService.addTzOrder(vo);
//    }
//
//    /**
//     * 编辑提重委托
//     */
//    @PostMapping("/updateTzOrder")
//    public ResultUtil<T> updateTzOrder(@RequestBody YwOrderVo vo, HttpServletRequest request) {
//        UserDto user = TokenUtil.getUser(request);
//        vo.setUpdateUserId(user.getId());
//        vo.setUpdateUserName(user.getRealname());
//        vo.setUpdateTime(LocalDateTime.now());
//        return ywOrderService.updateTzOrder(vo);
//    }

    /**
     * 根据车牌号获取委托信息
     */
    @PostMapping("/getYwOrderListByCarNo")
    public ResultUtil<List<YwOrderVo>> getYwOrderListByCarNo(@RequestBody YwOrderVo vo){
        Page<YwOrderVo> page = new Page<>(vo.getPageIndex(),vo.getPageSize());
        List<YwOrderVo> list = ywOrderService.getYwOrderListByCarNo(page,vo);
        return ResultUtil.build(list, ResultUtilCodeEnum.SUCCESS);
    }

    /**
     * 水运单导入
     * @return
     */
    @RequestMapping("/importYwOrderFile")
    public ResultUtil<List<TrainPlanDetailVo>> importYwOrderFile(YwOrderVo vo, @RequestParam(value = "fileRef") MultipartFile fileRef){
        vo.setFile(fileRef);
        return ywOrderService.importYwOrderFile(vo,ywOderFileUploadFolder,ywOderFileAccessPath);
    }

    /**
     * 铁路大票导入
     * @return
     */
    @RequestMapping("/importYwOrderBillNoFile")
    public ResultUtil<List<TrainPlanDetailVo>> importYwOrderBillNoFile(YwOrderVo vo, @RequestParam(value = "fileRef") MultipartFile fileRef){
        vo.setFile(fileRef);
        return ywOrderService.importYwOrderBillNoFile(vo,ywOderFileUploadFolder,ywOderFileAccessPath);
    }

    /**
     * 删除水运表
     */
    @RequestMapping("/delYwOrderFile")
    public ResultUtil<T> deleteYwOrderFile(@RequestBody YwOrderVo vo) {
        return ywOrderService.deleteYwOrderFile(vo.getId().intValue());
    }

    /**
     * yzs 散改集业务新增
     * @param request
     * @param vo
     * @return
     */
    @RequestMapping("/addSgjOrder")
    @SysTemLog(operModul = "业务管理-新增散改集业务",operType = "新增",operDesc = "新增散改集业务",modular="PC")
    public ResultUtil<T> addSgjOrder(HttpServletRequest request,@RequestBody YwOrderVo vo){
        UserDto user = TokenUtil.getUser(request);
        vo.setUserId(user.getId());
        vo.setUserName(user.getRealname());
        vo.setAccessPath("CMS");
        return ywOrderService.addCheckSgjOrder(vo);
    }

    /**
     * 查询
     */
    @RequestMapping("/getPowerOrderList")
    public ResultUtil<List<YwOrderVo>> getPowerOrderList(@RequestBody YwOrderVo vo) {
        Page<YwOrderVo> page = new Page<>(vo.getPageIndex(), vo.getPageSize());
        List<YwOrderVo> vos = ywOrderService.getCzPowerOrderList(page, vo);
        return ResultUtil.build(vos,page.getTotal(),page.getPages(),ResultUtilCodeEnum.SUCCESS);
    }

    /**
     * yzs 授权车辆
     */
    @RequestMapping("/addPowerCar")
    public ResultUtil<T> addPowerCar(HttpServletRequest request,@RequestBody YwOrderVo vo){
        Long userID = TokenUtil.getUserID(request);
        vo.setUserId(userID);
        vo.setUserName(sysUserService.getById(userID).getRealname());
        return ywOrderService.addCzPowerCar(vo);
    }

    /**
     * 查询
     */
    @RequestMapping("/getPowerCarList")
    public ResultUtil<List<YwOrderCarVo>> getPowerCarList(@RequestBody YwOrderVo vo) {
        Page<YwOrderCarVo> page = new Page<>(vo.getPageIndex(), vo.getPageSize());
        List<YwOrderCarVo> vos = ywOrderService.getPowerCarList(page, vo);
        return ResultUtil.build(vos,page.getTotal(),page.getPages(),ResultUtilCodeEnum.SUCCESS);
    }
}
